#include <android/log.h>
#include "com_bryandismas_pcsx_PcsxCore.h"
#include "PsxCommon.h"

#include <stdio.h>
#include <stdlib.h>

JNIEnv* env;
jobject androidBindings;
char dataDir[256];

JNIEXPORT void JNICALL Java_com_bryandismas_pcsx_PcsxCore_entryPoint(JNIEnv *ienv, jobject obj, jstring _dataDir)
{
	//__android_log_write(ANDROID_LOG_VERBOSE, "dlerror", (char*)dlerror());

	// hack to clean up dlerror
	while (dlerror() != 0)
	{
	}

	env = ienv;
	jclass cls = (*env)->GetObjectClass(env, obj);
	jfieldID fid = (*env)->GetFieldID(env, cls, "bindings", "Lcom/bryandismas/pcsx/AndroidBindings;");
	androidBindings = (*env)->GetObjectField(env, obj, fid);

	// get the data dir..
	const char* dir = (*env)->GetStringUTFChars(env, _dataDir, 0);
	strcpy(dataDir, dir);
	(*env)->ReleaseStringUTFChars(env, _dataDir, dir);

	// set up configuration.. this is pretty straight forward. since its a fixed set of plugins..
	memset(&Config, 0, sizeof(PcsxConfig));

	// copy default values into config..
	strcpy(Config.Gpu, "Disabled");
	strcpy(Config.Spu, "Disabled");
	strcpy(Config.Cdr, "libcdrbryoiso.so");
	strcpy(Config.Pad1, "Disabled");
	strcpy(Config.Pad2, "Disabled");
	strcpy(Config.Net, "Disabled");
	strcpy(Config.Mcd1, "/sdcard/pcsx/memcard/1.mcr");
	strcpy(Config.Mcd1, "/sdcard/pcsx/memcard/2.mcr");
	strcpy(Config.Bios, "HLE");
	strcpy(Config.BiosDir, "/sdcard/pcsx/bios/");
	strcpy(Config.PluginsDir, dataDir);
	strcpy(Config.Lang, "English");
	strcpy(Config.PARrom, "Disabled");
	Config.PsxAuto = 1;

	SysInit();
}
